/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.imoney.framework.base.exception.ServiceException;
import com.imoney.payment.member.common.InfoCode;
import com.imoney.payment.member.dao.IAddrDao;
import com.imoney.payment.member.domain.Addr;
import com.imoney.payment.member.service.IAddrService;

/**
 * 类说明：<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-23		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-23
 */
@Service(value = "addrService")
public class AddrServiceImpl implements IAddrService {

	private final static Logger logger = LoggerFactory
			.getLogger(AddrServiceImpl.class);

	// 批量处理条数
	private final int size = 50;

	@Resource
	private IAddrDao addrDao;

	@Override
	public void addAddr(Addr addr) throws ServiceException {
		try {
			addrDao.add(addr);
		} catch (Exception e) {
			logger.debug("AddrServiceImpl - create addr failture!", e);
			throw new ServiceException(InfoCode.INSERT_EXCEPTION, e);
		}
	}

	@Override
	public int updateAddr(Addr addr) throws ServiceException {
		try {
			return addrDao.update(addr);
		} catch (Exception e) {
			logger.debug("AddrServiceImpl - update addr failture!", e);
			throw new ServiceException(InfoCode.UPDATE_EXCEPTION, e);
		}
	}

	@Override
	public void addAddrList(List<Addr> addrList) throws ServiceException {
		int length = addrList.size();
		int ci = (length - 1) / size + 1;
		try {
			for (int i = 0; i < ci; i++) {
				int count = i * size;
				int toIndex = i < ci - 1 ? size * (i + 1) : length;
				addrDao.addAddrList(addrList.subList(count, toIndex));
			}
		} catch (Exception e) {
			logger.debug("AddrServiceImpl - create addr failture!", e);
			throw new ServiceException(InfoCode.INSERT_EXCEPTION, e);
		}
	}

}
